package Operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        //判断书架是否为空：
        int CurrentSide = bookList.getUseSide();
        if (CurrentSide == 0){
            System.out.println("书架上为空，不能删除图书！");
        }
        //判断删除的图书是否与书架上重名
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入你要删除的书名：");
        String name = scanner.nextLine();

        int index = -1;
        for (int i = 0; i < CurrentSide; i++) {
            Book book = bookList.getBooks(i);
            if (book.getName().equals(name)){
                index = i;
                break;
            }
        }
        if (index == -1){
            System.out.println("那么你的书架上没有你要删除的图书。");
        }

        for (int i = index; i < CurrentSide-1; i++) {
            Book book = bookList.getBooks(i+1);//这个时候通过后一项赋值给前一个值
            bookList.setBooks(i,book);//通过覆盖的方式来掩盖了被删除的图书
        }
        bookList.setUseSide(CurrentSide-1);
    }
}
